<template>
  <view>
        <view class="goods-item">
          <!-- 商品左侧的图片区域 -->
          <view class="goods-item-left">
              <!-- 左上角的勾选款  默认checked为勾选状态-->
              <radio :checked="goods.goods_state" color="#c00" v-if="showRadio" @click="radioChangeHandler"/>
            <image :src="goods.goods_small_logo || defaultPic" mode=""></image>
          </view>
          <!-- 商品右侧的文字区域 -->
          <view class="goods-list-right">
            <!-- 商品的标题 -->
            <view class="goods-name">{{goods.goods_name}}</view>
            <view class="prizeAndinput">
              <!-- 商品的价格 -->
              <view class="goods-price">￥ {{goods.goods_price | tofixed}}</view>
              <!-- 数字输入框 -->
              <uni-number-box :min="1" :value="goods.goods_count" v-if="showNum" @change="numberChange"></uni-number-box>
            </view>
          </view>
        </view>
  </view>
</template>

<script>
  export default {
     name:"my-goods",
    //自定义属性
    props: {
      goods:{
        type: Object,
        default: {}
      },
      // 是否显示购物车类的组件属性
      showRadio:{
        type:Boolean,
        //默认情况不进行显示
        default: false
      },
      // 是否显示 右侧数字输入框
      showNum:{
        type: Boolean,
        default: false,
      }
    },
    data() {
      return {
        //默认图片
        defaultPic: 'https://img3.doubanio.com/f/movie/8dd0c794499fe925ae2ae89ee30cd225750457b4/pics/movie/celebrity-default-medium.png'
      };
    },
    methods:{
      // radio组件的点击事件处理函数
      radioChangeHandler(){
         // 进行绑定外界的处理函数
        this.$emit('radio-change',{
          goods_id: this.goods.goods_id,
          goods_state: !this.goods.goods_state
        })
      },
      //监听到了NumberBox的组件数量的变化
      numberChange(val){
        console.log(val);
        this.$emit('num-change',{
          goods_id: this.goods.goods_id,
          goods_count: +val
        })
      }
    },
    // 过滤器节点,对价格进行添加小数点
    filters:{
      tofixed(num){
        // 对价格添加2位小数
        return Number(num).toFixed(2)
      } 
    }
  }
</script>

<style lang="scss">
.goods-item{
  width: 750px;
  box-sizing: border-box;
  display: flex;
  padding: 10px 5px ;
  border-bottom: 1px solid #efefef;
  // 左侧图片区域
  .goods-item-left{
    margin-right: 5px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    image{
      width: 100px;
      height: 100px;
      display: block;
    }
  }
  //右侧文字区域
  .goods-list-right{
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    .goods-name{
      width: 225px;
      font-size: 13px;
    }
    .prizeAndinput{
      width: 225px;
      display: flex;
      justify-content: space-between;
      .goods-price{
        color: #c00;
        font-size: 16px;
      }
    }
  }
}

</style>